In [3]:
import csv
from collections import Counter
from lib.users import UserAPI
from lib.geo import geocode
api=UserAPI()
tweets_file="/home/clemsos/Dev/mitras/data/sampleweibo.csv"
#tweets_file="/home/clemsos/Dev/mitras/lib/cities/usersample.csv"
user_provinces=[]
with open(tweets_file, 'rb') as csvfile:
weibo_data=csv.reader(csvfile)
csvfile.next() #skip csv header
for tweet in weibo_data:
# print tweet[2]
province_code= api.get_province(tweet[2])
user_provinces.append(api.provinces[province_code])
province_count=Counter(user_provinces)
print province_count.most_common()
for p in province_count.most_common():
print p[0],p[1]
# codes=[]
# for province in province_count:
# code=geocode(province)
# codes.append(code)
# print codes
In [1]:
from kartograph import Kartograph
K = Kartograph()
K.generate(config, outfile='mymap.svg')
# bash commands to decrease shapefile size
# ogr2ogr -clipsrc -3 37 4 44 Trees2.shp Trees.shp
# ogr2ogr -simplify 10 munis.shp bm50mv33sh1fpm1r170.shp
In [2]:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from matplotlib.colors import rgb2hex
from matplotlib.patches import Polygon
import os
In [32]:
shp_info_path ="/home/clemsos/Dev/mitras/data/shp/map_china/CHN_adm1"
print shp_info_path
# Lambert Conformal map of China
# "Base 802438 (545114) 2-96" Relief shown by shading. Scale [ca. 1:30,000,000] ;
# Lambert conformal conic proj. (E 750--E 1350/N 470--N 150).
# m = Basemap(width=400000,height=400000,projection='lcc',resolution='f',lon_0=105.,lat_0=35.0)
# (lon, lat)
austin = (-97.75, 30.25)
hawaii = (-157.8, 21.3)
hongkong = (114.16, 22.28)
moscow = (37.62, 55.75)
havana = (-82.38, 23.13)
quito = (-78.58, -0.25)
land_color = 'lightgray'
water_color = 'lightblue'
# fig, ax = subplots(figsize=(12,12))
map = Basemap(projection='merc', llcrnrlat=-80, urcrnrlat=80,
llcrnrlon=-180, urcrnrlon=180, resolution='l')
map.fillcontinents(color=land_color, lake_color=water_color)
map.drawcoastlines()
map.drawparallels(np.arange(-90.,120.,30.))
map.drawmeridians(np.arange(0.,420.,60.))
map.drawmapboundary(fill_color=water_color)
ax.set_title('Mercator')
map.ax = ax
# choose a color for each state based on population density.
colors={}
statenames=[]
cmap = plt.cm.hot # use 'hot' colormap
vmin = 0; vmax = 450 # set range.
print(m.states_info[0].keys())
x, y = map(*zip(*[hawaii, hongkong, moscow, havana, quito]))
map.plot(x, y, marker='o', markersize=6, markerfacecolor='black', linewidth=0)
#shp_info = m.readshapefile(shp_info_path,'states',drawbounds=True)
#print(shp_info)
In [9]:
from IPython.display import display
from lib.ipyD3 import *
from pandas import *
import numpy as np
d3 = d3object(width=800,
height=500,
style='JFFigure',
number=1,
title='Unemployment',
desc='Oct 2012')
d3.addJs('''
var svg = d3.select("#"+d3ObjId)
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("id", "#"+d3ObjId+'InnerG')
// data
var umap = []
data.map(function(d) {umap[d[0]]=Number(d[1])});
console.log(umap);
var v = Object.keys(umap).map(function(k){return umap[k]})
console.log(v);
var projection = d3.geo.mercator()
.center([116,39])
.scale(600);
var svg = d3.select("#map").append("svg")
.attr("width", width)
.attr("height", height)
.attr("preserveAspectRatio", "xMidYMid")
.attr("viewBox", "0 0 " + width + " " + height);
// color scale
var colorScale = d3.scale.linear()
.domain(d3.extent(v))
.interpolate(d3.interpolateHcl)
.range(["orange", "green"]);
var color = function(i){
if (i==undefined) {return "#cccccc"}
else return colorScale(i)
}
// LEGEND
// Scale for color bar
svg.selectAll("rect")
.data(d3.range(d3.min(v), d3.max(v), (d3.max(v)-d3.min(v))/50.0))
.enter()
.append("rect")
.attr({width: 25,
height: 5,
y: function(d,i) { return height-25-i*5 },
x: width-50,
fill: function(d,i) { return color(d); } })
svg.append("g")
.attr("transform", "translate(" + (width-25) + "," + (height-25-5*49) + ")")
.call(d3.svg.axis()
.scale(d3.scale.linear().domain(d3.extent(v)).range([5*50,0]))
.orient("right"));
// var svg = d3.select("#map").append("svg")
//
// .attr("width", m_width)
// .attr("height", m_width * height / width);
// svg.append("rect")
// .attr("class", "background")
// .attr("width", width)
// .attr("height", height);
// // .on("click", country_clicked)
// Color bar
// Adapted from http://tributary.io/tributary/3650755/
var path = d3.geo.path()
.projection(projection);
// Chinese provinces
d3.json("https://raw.githubusercontent.com/clemsos/mitras/master/lib/maps/zh-mainland-provinces.topo.json", function(error, cn) {
var codes=[];
for (var i = 0; i < topojson.feature(cn, cn.objects.provinces).features.length; i++) {
codes.push(topojson.feature(cn, cn.objects.provinces).features[i].properties.name)
};
console.log(codes);
svg.append("g")
.attr("class", "provinces")
.selectAll("path")
.data(topojson.feature(cn, cn.objects.provinces).features)
.enter()
.append("path")
.attr("d", path)
.attr("id", function(d) { return d.id; })
.attr("class", "province")
// .attr("fill", function(d) { return color(d.id); })
.attr("fill", "#cccccc")
.attr("fill", function(d) { return color(umap[d.properties.name]); })
.attr("stroke", "black")
.attr("stroke-width", "0.35");
});
// Add Taiwan
d3.json("https://raw.githubusercontent.com/clemsos/mitras/master/lib/maps/zh-countries.topo.json", function(error, cn) {
svg.append("g")
.attr("class", "tw")
.attr("class", "provinces")
.selectAll("path")
.data(topojson.feature(cn, cn.objects.countries).features.filter(function(d) { return d.id === 2; }))
.enter()
.append("path")
.attr("d", path)
.attr("id", function(d) { return d.id; })
.attr("class", "province")
.attr("fill", "#cccccc")
.attr("fill", function(d) { return color(umap["Táiwān"]); })
.attr("stroke", "black")
.attr("stroke-width", "0.35");
});
// Add HK
d3.json("https://raw.githubusercontent.com/clemsos/mitras/master/lib/maps/zh-countries.topo.json", function(error, cn) {
var svgHK = d3.select("#map").append("svg")
.attr("class", "hk")
.attr("width", 100)
.attr("height", 100)
var projection2 = d3.geo.mercator()
.center([126,17])
.scale(2000);
var path2 = d3.geo.path()
.projection(projection2);
svgHK.append("g")
// .attr("class", "provinces")
.selectAll("path")
.data(topojson.feature(cn, cn.objects.countries).features.filter(function(d) { return d.id === 1; }))
.enter()
.append("path")
.attr("d", path2)
.attr("id", function(d) { return d.id; })
.attr("class", "province")
.attr("fill", function(d) { return color(umap["Xiānggǎng"]); })
.attr("stroke", "black")
.attr("stroke-width", "0.35");
svgHK.append("text") //add some text
.attr("dx", function(d){return 20})
.attr("dy", function(d){return 35})
.attr("font-family", "sans-serif")
.attr("fill", "#aaaaaa")
.attr("font-size", 10)
.text("Hong Kong")
});
//TODO Aomen / Haiwai
''')
data=[ [p[0],p[1]] for p in province_count.most_common() ]
data
d3.addVar(data=data)
html=d3.render(mode=('show','html'))
display(html)
In [ ]: